package com.forotuxpan.appforotuxpan;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.AsyncTask;
import android.os.Bundle;
//import android.support.v4.app.Fragment;
import android.app.Fragment;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * Created by gmartinezh on 21/04/2015.
 */
public class BaseFragment extends Fragment {
    public static final String nombreBD = "bs";
    public SQLiteDatabase db;
    // private static final String tblSetting = "Setting";
    // private static final String createtblSetting = "create table if not exists "
    //        + tblSetting + " (IdSetting integer primary key autoincrement, FechaFur text, FechaFbs text, NombreBebe Text);";

    ProgressDialog progressDialog = null;
    //public static final String MY_AD_UNIT_ID="ca-app-pub-3357081846998776/9001617244";
    public AsyncTask<Void, Void, Void> task;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    public void hiddenLoading() {
        progressDialog.dismiss();
    }

    public void showLoading(Context context) {
        progressDialog = new ProgressDialog(context);
        progressDialog.show();
        progressDialog.setContentView(R.layout.custom_progressdialog);
        progressDialog.setCancelable(true);//se podra cerrar simplemente pulsando back
        progressDialog.setOnCancelListener(new DialogInterface.OnCancelListener() {
            @Override
            public void onCancel(DialogInterface dialog) {
                if (task != null) {
                    if (task.getStatus() == AsyncTask.Status.RUNNING) {
                        task.cancel(true);
                    }
                }
            }
        });
    }

    public int Dateiff(Calendar c1, Calendar c2) {
        Date d1=c1.getTime();
        Date d2=c2.getTime();

        long diff=d1.getTime()-d2.getTime();
        return (int)(diff/(1000*24*60*60));
    }

    /*
    public Calendar getSettingsFbs(Context context) {
        BabyShowerSQLiteHelper usdbh = new BabyShowerSQLiteHelper(context, nombreBD, null, 1);
        db = usdbh.getWritableDatabase();

        db.execSQL(createtblSetting);
        Calendar calendar = Calendar.getInstance();
        Cursor c = db.rawQuery("SELECT FechaFbs FROM " + tblSetting, null);
        if (c.moveToFirst()) {
            do {
                SimpleDateFormat formatoDelTexto = new SimpleDateFormat("dd/MM/yyyy");
                Date fecha = null;
                try {
                    fecha = formatoDelTexto.parse(c.getString(0));
                    calendar.setTime(fecha);

                } catch (ParseException e) {
                    e.printStackTrace();
                }
            } while (c.moveToNext());
        }

        db.close();
        return calendar;
    }
*/
    public Calendar getDate() {
        SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");
        Calendar now = Calendar.getInstance();
        Calendar calendar = Calendar.getInstance();
        String date = formatter.format(now.getTime());
        Date fecha = null;
        try {
            fecha = formatter.parse(date);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        calendar.setTime(fecha);
        return calendar;
    }
}
